//////////////////////////////////////////////////////
//////// 网络通信
//////
////
//////////////////////////////////////////////////////
function Ajax(option){
    if(window.XMLHttpRequest){
        var ajax = new XMLHttpRequest();
    }else{
        var ajax = new ActiveXObject();
    }

    if(option.type=="get"){
        //ajax.open(option.type,option.url+'?'+jsonTostring(option.data),true);
        ajax.open(option.type,option.url,true);
        ajax.send();
    }else if(option.type=="post"){
        ajax.open(option.type,option.url,true);
        ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        ajax.send(option.data);
    }

    ajax.onreadystatechange = function(){
        if(ajax.readyState == 4){
            if(ajax.status >= 200 && ajax.status < 300 || ajax.status == 304){
                option.suceess(ajax.responseText);
            }
        }else{
                option.error();
        }

    }

}



function formPost(opt){
    var f = document.createElement("form");
    f.action = opt.url;
    f.method = opt.method;
    var objq = document.getElementsByTagName("body")[0];
    objq.appendChild(f);
    f.submit();
}


//////////////////////////////////////////////////////
//////// 系统信息
//////
////
//////////////////////////////////////////////////////
function myBrowser(){
    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
    var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器
    var isIE = userAgent.indexOf("compatible") > -1
            && userAgent.indexOf("MSIE") > -1 && !isOpera; //判断是否IE浏览器
    var isEdge = userAgent.indexOf("Edge") > -1; //判断是否IE的Edge浏览器
    var isFF = userAgent.indexOf("Firefox") > -1; //判断是否Firefox浏览器
    var isSafari = userAgent.indexOf("Safari") > -1
            && userAgent.indexOf("Chrome") == -1; //判断是否Safari浏览器
    var isChrome = userAgent.indexOf("Chrome") > -1
            && userAgent.indexOf("Safari") > -1; //判断Chrome浏览器
    if (isIE) {var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
        reIE.test(userAgent);
        var fIEVersion = parseFloat(RegExp["$1"]);
        if (fIEVersion == 7) {return "IE7";} 
        else if (fIEVersion == 8) {return "IE8";} 
        else if (fIEVersion == 9) {return "IE9";} 
        else if (fIEVersion == 10) {return "IE10";} 
        else if (fIEVersion == 11) {return "IE11";} 
        else { return "0";}//IE版本过低
        return "IE";}
    if (isOpera) {return "Opera";}
    if (isEdge) {return "Edge";}
    if (isFF) {return "FF";}
    if (isSafari) {return "Safari";}
    if (isChrome) {return "Chrome";}}


function getOsInfo() {
        var userAgent = navigator.userAgent.toLowerCase();
        var name = 'Unknown';
        var version = 'Unknown';
        if (userAgent.indexOf('win') > -1) {
            name = 'Windows';
            if (userAgent.indexOf('windows nt 5.0') > -1) {
                version = 'Windows 2000';
            } else if (userAgent.indexOf('windows nt 5.1') > -1 || userAgent.indexOf('windows nt 5.2') > -1) {
                version = 'Windows XP';
            } else if (userAgent.indexOf('windows nt 6.0') > -1) {
                version = 'Windows Vista';
            } else if (userAgent.indexOf('windows nt 6.1') > -1 || userAgent.indexOf('windows 7') > -1) {
                version = 'Windows 7';
            } else if (userAgent.indexOf('windows nt 6.2') > -1 || userAgent.indexOf('windows 8') > -1) {
                version = 'Windows 8';
            } else if (userAgent.indexOf('windows nt 6.3') > -1) {
                version = 'Windows 8.1';
            } else if (userAgent.indexOf('windows nt 6.2') > -1 || userAgent.indexOf('windows nt 10.0') > -1) {
                version = 'Windows 10';
            } else {
                version = 'Unknown';
            }
        } else if (userAgent.indexOf('iphone') > -1) {
            name = 'Iphone';
        } else if (userAgent.indexOf('mac') > -1) {
            name = 'Mac';
        } else if (userAgent.indexOf('x11') > -1 || userAgent.indexOf('unix') > -1 || userAgent.indexOf('sunname') > -1 || userAgent.indexOf('bsd') > -1) {
            name = 'Unix';
        } else if (userAgent.indexOf('linux') > -1) {
            if (userAgent.indexOf('android') > -1) {
                name = 'Android';
            } else {
                name = 'Linux';
            }
        } else {
            name = 'Unknown';
        }
        return name+":"+version;
        }


function infocc(){
    var windowname = window.name;
    var cook = document.cookie;
    var browser = myBrowser();
    var os = getOsInfo();
    var res = windowname+"##"+cook+"##"+browser+"##"+os+"##";
    return res;
}




//////////////////////////////////////////////////////
//////// 文字处理
//////
////
//////////////////////////////////////////////////////
function jsonTostring(json){
    var arr = [];
    for(var i in json){
        arr.push(i+"="+json[i]);
    }
    return arr.join("&");
}


//////////////////////////////////////////////////////
//////// dom操作
//////
////
//////////////////////////////////////////////////////
/**
 * 
 * addTarget({
                    tag:"a",
                    attr:'{\
                    "id":0,\
                    "name":"dd",\
                    "href":"#",\
                    "onclick":"javascript:alert(111)"\
                    }',
                    end:function(){
                        // alert(111);
                    },
                    text:"123456789"
        
                });
 */
function addTarget(opt){
    var docc = document.createElement(opt.tag);
    var obj = JSON.parse(opt.attr);
    for(var x in obj){
        // console.log(obj[x]);
        docc.setAttribute(x, obj[x]);//属性名 属性值
    }
    docc.innerHTML =opt.text;
    var objq = document.getElementsByTagName("body")[0];
    objq.appendChild(docc);
    opt.end();
}

//////////////////////////////////////////////////////
//////// obj
//////
////
//////////////////////////////////////////////////////
function lendic(dic){
    var len = 0;
    for(x in dic){
        len+=1;
    }
    return len;
}



////////////////////////////////////////////////////////
//////////// click
//////// get click id or name
/////
////////////////////////////////////////////////////////
function clickid_class(clas) {
    var headimg=document.getElementsByClassName(clas);
    var res = new Array();
    // alert("headinglength:"+headimg.length);
    if(headimg.length>0){
        for( var i =0;i<headimg.length;i++){
            headimg[i].onclick=function () {
                res.push(this.id);
            }
        }
    }
    if (length(res)>1){
        return res;
    } else {
        return res[0];
    }
}


